package com.lx.test.decry;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lX
 * @version JDK 8
 * @className DecryptFilter (此处以class为例)
 * @date 2024/6/12
 * @description TODO
 */
public class DecryptFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        DecryRequestWrapper decryRequestWrapper = new DecryRequestWrapper((HttpServletRequest) servletRequest);


        Map<String, String[]> parameterMap = new HashMap<>(decryRequestWrapper.getParameterMap());

        //1、值类型参数修改
        ///对地址栏参数进行解密重新put到parameterMap中
        parameterMap.put("id", new String[]{"2"});
        decryRequestWrapper.setParameterMap(parameterMap);
        //对requestbody的参数进行解密，然后重新setbody中
        //2、body类型修改
        String jsonData="{\"x\":1,\"y\":2}";
        byte[] reqBodyBytes = jsonData.getBytes();
        decryRequestWrapper.getBody();
        decryRequestWrapper.setBody(reqBodyBytes);
        //使用复写后的wrapper
        filterChain.doFilter(decryRequestWrapper, servletResponse);
    }
}
